/**
 * 
 */
package com.ermax.aircond.common.dao.impl;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.ermax.aircond.common.dao.WriteMachineDataDao;
import com.ermax.aircond.common.domain.WriteMachineData;
import com.ermax.aircond.util.dao.AbstractHibernateDaoImpl;

/**
 * @author Eric
 *
 */
@Repository("writeMachineDataDao")
public class WriteMachineDataDaoImpl extends AbstractHibernateDaoImpl<WriteMachineData> implements WriteMachineDataDao {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1418603846702420259L;

	private static final Logger logger = Logger.getLogger(WriteMachineDataDaoImpl.class);
	
	private WriteMachineDataDaoImpl() {
		setClazz(WriteMachineData.class);
	}
	
	/* (non-Javadoc)
	 * @see com.ermax.aircond.common.dao.WriteMachineDataDao#create(com.ermax.aircond.common.domain.WriteMachineData)
	 */
	@Override
	public void insert(WriteMachineData writeMachineData) {
		this.create(writeMachineData);
	}

	@Override	
	public List<WriteMachineData> listData() {
		
		return this.findAll();
		
	}

	@Override
	public WriteMachineData select(short nid) {
		
		Criteria criteria = getCurrentSession().createCriteria(WriteMachineData.class);
		if(isNotNull(nid)){
			criteria.add(Restrictions.eq("nid", nid));
		}
		
		List<WriteMachineData> results = criteria.list();
		if(results.size() > 0){
			return results.get(0);
		}
		
		return null;	
		
	}
	
	@Override
	public void update(WriteMachineData writeMachineData) {
		logger.debug("Write Machine Data: " + writeMachineData);
		super.update(writeMachineData);		
		
		WriteMachineData zeroData = new WriteMachineData();
		/*
		try {
			logger.debug("Wait for 8 seconds!");
			Thread.sleep(8000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			logger.error(e.getLocalizedMessage());
		}
		
		//
		zeroData.setNid(writeMachineData.getNid());
		zeroData.setCmd("0");
		//
		
		logger.debug("Write Zero Data: " + zeroData);
		super.update(zeroData);
		*/
		
		
		/* Timer Task
		Timer timer = new Timer();
		
		WriteMachineDataTimerTask timerTask = new WriteMachineDataTimerTask();
		timerTask.setData(zeroData);
		
		timer.schedule(timerTask, 4000);
		*/
		
		/*
		WriteMachineData zeroData = new WriteMachineData();
		zeroData.setNid(writeMachineData.getNid());
		
		try {
			logger.debug("Wait for 3 seconds!");
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			logger.info("Interrupted when doing set 0!");
			
		}
		
		zeroData.setCmd("0");
		super.update(zeroData);
		*/
	}
	


}
